{
 "cells": [
  {
   "cell_type": "raw",
   "id": "77dd0c90-94d7-4acd-a360-e977b39d0a8f",
   "metadata": {},
   "source": [
    "---\n",
    "sidebar_position: 0\n",
    "title: Quick Start\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d98412d-fc53-42c1-aed8-f1f8eb9ada58",
   "metadata": {},
   "source": [
    "# Quick Start\n",
    "Prompt templates are predefined recipes for generating prompts for language models.\n",
    "\n",
    "A template may include instructions, few-shot examples, and specific context and\n",
    "questions appropriate for a given task.\n",
    "\n",
    "LangChain provides tooling to create and work with prompt templates.\n",
    "\n",
    "LangChain strives to create model agnostic templates to make it easy to reuse\n",
    "existing templates across different language models.\n",
    "\n",
    "Typically, language models expect the prompt to either be a string or else a list of chat messages.\n",
    "\n",
    "## `PromptTemplate`\n",
    "\n",
    "Use `PromptTemplate` to create a template for a string prompt.\n",
    "\n",
    "By default, `PromptTemplate` uses [Python's str.format](https://docs.python.org/3/library/stdtypes.html#str.format)\n",
    "syntax for templating."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a5bc258b-87d2-486b-9785-edf5b23fd179",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tell me a funny joke about chickens.'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "prompt_template = PromptTemplate.from_template(\n",
    "    \"Tell me a {adjective} joke about {content}.\"\n",
    ")\n",
    "prompt_template.format(adjective=\"funny\", content=\"chickens\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d54c803c-0f80-412d-9156-b8390e0265c0",
   "metadata": {},
   "source": [
    "The template supports any number of variables, including no variables:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "63bd7ac3-5cf6-4eb2-8205-d1a01029b56a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tell me a joke'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "prompt_template = PromptTemplate.from_template(\"Tell me a joke\")\n",
    "prompt_template.format()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2715fd80-e294-49ca-9fc2-5a012949ed8a",
   "metadata": {},
   "source": [
    "You can create custom prompt templates that format the prompt in any way you want.\n",
    "For more information, see [Custom Prompt Templates](./custom_prompt_template.html).\n",
    "\n",
    "## `ChatPromptTemplate`\n",
    "\n",
    "The prompt to [chat models](../models/chat) is a list of chat messages.\n",
    "\n",
    "Each chat message is associated with content, and an additional parameter called `role`.\n",
    "For example, in the OpenAI [Chat Completions API](https://platform.openai.com/docs/guides/chat/introduction), a chat message can be associated with an AI assistant, a human or a system role.\n",
    "\n",
    "Create a chat prompt template like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d088d53c-0e20-4fb9-9d54-b0e989b998b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import ChatPromptTemplate\n",
    "\n",
    "chat_template = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", \"You are a helpful AI bot. Your name is {name}.\"),\n",
    "        (\"human\", \"Hello, how are you doing?\"),\n",
    "        (\"ai\", \"I'm doing well, thanks!\"),\n",
    "        (\"human\", \"{user_input}\"),\n",
    "    ]\n",
    ")\n",
    "\n",
    "messages = chat_template.format_messages(name=\"Bob\", user_input=\"What is your name?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1e7e3ef-ba7d-4ca5-a95c-a0488c9679e5",
   "metadata": {},
   "source": [
    "`ChatPromptTemplate.from_messages` accepts a variety of message representations.\n",
    "\n",
    "For example, in addition to using the 2-tuple representation of (type, content) used\n",
    "above, you could pass in an instance of `MessagePromptTemplate` or `BaseMessage`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f6632eda-582f-4f29-882f-108587f0397c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[SystemMessage(content=\"You are a helpful assistant that re-writes the user's text to sound more upbeat.\"), HumanMessage(content=\"I don't like eating tasty things\")]\n"
     ]
    }
   ],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.prompts import HumanMessagePromptTemplate\n",
    "from langchain_core.messages import SystemMessage\n",
    "\n",
    "chat_template = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        SystemMessage(\n",
    "            content=(\n",
    "                \"You are a helpful assistant that re-writes the user's text to \"\n",
    "                \"sound more upbeat.\"\n",
    "            )\n",
    "        ),\n",
    "        HumanMessagePromptTemplate.from_template(\"{text}\"),\n",
    "    ]\n",
    ")\n",
    "messages = chat_template.format_messages(text=\"I don't like eating tasty things\")\n",
    "print(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c4b46da-d51b-4801-955f-ba4bf139162f",
   "metadata": {},
   "source": [
    "This provides you with a lot of flexibility in how you construct your chat prompts."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a5fe78c-572c-4e87-b02f-7d33126fb605",
   "metadata": {},
   "source": [
    "## LCEL\n",
    "\n",
    "`PromptTemplate` and `ChatPromptTemplate` implement the [Runnable interface](/docs/expression_language/interface), the basic building block of the [LangChain Expression Language (LCEL)](/docs/expression_language/). This means they support `invoke`, `ainvoke`, `stream`, `astream`, `batch`, `abatch`, `astream_log` calls.\n",
    "\n",
    "`PromptTemplate` accepts a dictionary (of the prompt variables) and returns a `StringPromptValue`. A `ChatPromptTemplate` accepts a dictionary and returns a `ChatPromptValue`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0f0e860b-95e0-4653-8bab-c5d58b0f7d67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StringPromptValue(text='Tell me a joke')"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt_val = prompt_template.invoke({\"adjective\": \"funny\", \"content\": \"chickens\"})\n",
    "prompt_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c0dac782-5144-4489-8d77-eba47f1cd1c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tell me a joke'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt_val.to_string()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a8e3ac32-f690-4d3d-bcb2-27b7931beab2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HumanMessage(content='Tell me a joke')]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt_val.to_messages()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4516257f-0c3b-4851-9e82-8c9e09111444",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat_val = chat_template.invoke({\"text\": \"i dont like eating tasty things.\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "7adfe927-ba1d-425f-904c-0328e1a10c18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[SystemMessage(content=\"You are a helpful assistant that re-writes the user's text to sound more upbeat.\"),\n",
       " HumanMessage(content='i dont like eating tasty things.')]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat_val.to_messages()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "37c9e2e4-a2e8-48a9-a732-01c025a21362",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"System: You are a helpful assistant that re-writes the user's text to sound more upbeat.\\nHuman: i dont like eating tasty things.\""
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat_val.to_string()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
